package com.atguigu.juc.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @author yfstart
 * @create 2022-06-20 11:33
 */
public class CompletableFutureAPIDemo2 {
  public static void main(String[] args) throws ExecutionException, InterruptedException {
      CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
          try {
              TimeUnit.MILLISECONDS.sleep(2000);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
          return "hello supplyAsync";
      });

      // 注释掉暂停线程，get还没有算完只能返回complete方法设置的complete；暂停3秒钟线程，异步线程能够计算完成返回get
      try {
        TimeUnit.MILLISECONDS.sleep(3000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      // 当调用CompletableFuture.get()被阻塞的时候,complete方法就是结束阻塞并get()获取设置的complete里面的值.
      System.out.println(completableFuture.complete("complete")+"\t"+completableFuture.get());
  }
}
